const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const tool = require("../../tools/tool");
const router = new Router();
// 提交购物车
router.post('/app/getSaleList', async ctx => {
    try {
        let user = ctx.user;
        let tenantId = user.tenantId;
        let uid = user.id;
        let body = ctx.request.body;
        let currentPage= body.currentPage;
        let beginDate= body.beginDate;
        let endDate= body.endDate;
        let salesName= body.salesName;
        let buName= body.buName;
        let invName= body.invName;
        let billState= body.billState?body.billState:'';
        let searchType= body.searchType?body.searchType:'quick';
        let keyword= body.keyWord?body.keyWord:'';
        let pageSize= body.pageSize?body.pageSize:30;
        let offset = (currentPage - 1) * pageSize;
        let sql = `select tenantId, billStateSort,id,billDate,billState,billStateName,billNo,salesId,salesName,cby,cbyName,amount,buId,buName,address, remark,
                            isnull(hxAmount,0) hxAmount,isnull(leftYe,0) leftYe from v_main_withhx 
                            where  tenantId=${tenantId} and transType=150601 and billState<>'del' 
                          `
        if(searchType === 'quick') {
            if(keyword) {
                sql += ` and (buName like '%${keyword}%' or address like '%${keyword}%' or goods like '%${keyword}%') `
            }
            sql += ` and salesName='${user.name}' `
        }
        else {
            if(billState === 'draft') {
                sql += ` and billState='draft' `
            }
            if(billState === 'accounted') {
                sql += ` and billState='accounted' `
            }
            if(billState === 'partReceipt') {
                sql += ` and leftYe<amount and leftYe <> 0 `
            }
            if(billState === 'hasLeft') {
                sql += ` and  leftYe <> 0 `
            }
            if(beginDate) {
                sql += ` and billDate>='${beginDate}' `
            }
            if(endDate) {
                sql += ` and billDate<='${endDate}' `
            }
            if(buName) {
                sql += ` and buName like '%${buName}%' `
            }
            if(salesName) {
                sql += ` and salesName='${salesName}' `
            }
            else {
                sql += ` and salesName='${user.name}' `
            }
        }
        if(invName) {
            sql = `select tenantId, billStateSort,id,billDate,billState,billStateName,billNo,salesId,salesName,cby,cbyName,amount,buId,buName,address, remark, hxAmount,leftYe
            from (select distinct a.*
                  from (${sql}) a
                           inner join invoice_info b on a.tenantId = b.tenantId and a.id = b.iid
                           inner join goods c on b.invId = c.id and b.tenantId = c.tenantId
                  where c.name like '%${invName}%'
            ) a `
        }
        sql += ' order by  billStateSort, billDate desc, id  '
        sql += ` offset ${offset} rows fetch next ${pageSize} rows only; `
        let invoice = await db.query(sql, {type: QueryTypes.SELECT})
        invoice.map(row => {
            row.billStateName = row.billStateName === '草稿'?'待审核':row.billStateName
            row.amount = tool.xe.toFixed(row.amount, 2)
            row.skState = '未收款'
            if(tool.xe.toNumber(row.printNo) !== 0) {
                row.printState = '已打印'
            }
            else {
                row.printState = '未打印'
            }
            if(tool.xe.toNumber(row.leftYe) <tool.xe.toNumber(row.amount) && tool.xe.toNumber(row.leftYe)!==0) row.skState = `部分收款(${tool.xe.toFixed(row.hxAmount,2)})`
            if(tool.xe.toNumber(row.leftYe) == 0) row.skState = '已收款'
        })
        return ctx.suc('', {
            total: 0,
            list: invoice
        })
    } catch (e) {
        return ctx.err(e)
    }

})

module.exports = router;
